/*
 * @Author: 何丽彬
 * @Email: helibin@yingzi.com
 * @Date: 2021-11-18 11:30:46
 * @LastEditTime: 2021-11-18 11:30:47
 * @LastEditors: 何丽彬
 * @FilePath: \process-learn\fork_compute.js
 */
const computation = () => {
  let sum = 0;
  console.info("计算开始");
  console.time("计算耗时");

  for (let i = 0; i < 1e10; i++) {
    sum += i;
  }

  console.info("计算结束");
  console.timeEnd("计算耗时");
  return sum;
};

process.on("message", (msg) => {
  console.log(msg, "process.pid", process.pid); // 子进程id
  const sum = computation();

  // 如果Node.js进程是通过进程间通信产生的，那么，process.send()方法可以用来给父进程发送消息
  process.send(sum);
});
